//显示图片
function show_photo(node) {
	var _this = $(node);
	val = $.trim(_this.val());
    $.ajax({
      url:"/dashboard/upload/show_photo",
      type:"POST",
      data:{cover:val},
      success:function(res) {
        res=$.parseJSON(res);
        _this.parents('.all-img').prepend(res);
      }
    })
}

//图片移除操作
function del_photo(e) {
    var _this = $(e);
    var _that = _this.parents('.all-img').find('input');
    var id = _this.data('id');
    var upload_arr = _that.val().split(',');
    var arr = [];
    $.each(upload_arr,function(i,j) {
      if(j!=id){
        arr.push(j);
      }
    })
    _that.val(arr)
    _this.parent().remove();
}

//上传图片
function upload_photo(e) {
	$(e).AjaxFileUpload({
      action: "cover_upload",
      onComplete: function(filename, response) {
        var that = $(this);
        var upload_val = $('#' + that.attr('data-id')).val();
        console.log(response)
        if (response.ok == 1) {                                            //多张图片   
              //未上传图片时
              if(upload_val.length==0){
                  $('#' + that.attr('data-id')).val(response.insert_id);
  	              var res = '<div class="upload-box form-img-added"><img class="upload-img" src="' + response.fileurl + '"><div class="del-btn" onclick="return del_photo(this);" data-id = "' + response.insert_id + '">删除</div></div>';
  	              that.parents('.input-style').next('.all-img').prepend(res);
              //已上传图片
              }else{
    		          // 判断是否是多张图片上传
    		          if(that.data('amount')==0){                                    //单张图片
    		              $('#' + that.attr('data-id')).val(response.insert_id);
    		              $('label#' + that.attr('id') + "-for").prev('.upload-box').html('<img class="upload-img" src="' + response.fileurl + '" /><div class="del-btn" onclick="return del_photo(this);" data-id = "' + response.insert_id + '">删除</div></div>');
    		          }else{             
    		              $('#' + that.attr('data-id')).val(upload_val + ',' + response.insert_id);
    		              var res = '<div class="upload-box form-img-added"><img class="upload-img" src="' + response.fileurl + '"><div class="del-btn" onclick="return del_photo(this);" data-id = "' + response.insert_id + '">删除</div></div>';
    		              that.parents('.input-style').next('.all-img').prepend(res);
    		          }
              }
        } else {
          alert(response.msg);
          // $('.alert-error p').html(response.msg);
          // $('.alert-error').slideDown('fast');
          // setTimeout (function() {
          //   $('.alert-error').slideUp('fast');
          // },3000)
        }
      }
    });
}